package com.demoo.zjsu.service.smartwebsite;

import com.demoo.zjsu.dao.smartwebsite.SmartWebDAO;
import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.util.smartwebsite.WebsiteUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/16.
 * 多对多的链接页面
 */
@Service
public abstract class PageListFactory extends PageFactory{

    @Autowired
    SmartWebDAO dao;

    @Override
    public void resolver() {
        Page page = getPage();
        String[] urls = page.getLinkUrls();
        String[] categories = page.getCategories();
        int size = urls.length;
        List<PageTwo> pageTwos = new ArrayList<PageTwo>();
        List<PageTwoContent> contents = new ArrayList<PageTwoContent>();
        for (int i = 0; i < size; i++) {
            Document document = WebsiteUtil.getDocByURL(urls[i]);
            Elements elements = getMain(document);
            filterElse(elements);
            packageContent(elements, pageTwos, contents, categories[i]);
        }
        dao.insertListPageTwo(pageTwos);
        dao.insertListPageTwoContent(contents);
    }


}
